package com.lovo.project.servlet.product;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lovo.project.enums.ProductEnum;
import com.lovo.project.exception.OurException;
import com.lovo.project.model.ProductDO;
import com.lovo.project.service.ProductService;
import com.lovo.project.service.impl.ProductServiceImpl;
import com.lovo.project.vo.ResultVO;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

/**
 * @title 所有商品url
 * @author: chenqiang
 * @date: 2020/11/4 14:18
 */
@WebServlet(name = "ProductAllServlet",value = "/product/productAll")
public class ProductAllServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            request.setCharacterEncoding("utf-8");
            response.setCharacterEncoding("utf-8");
            response.setContentType("text/html; charset=UTF-8");


            String productName =request.getParameter("productName");
            Integer shopId =Integer.parseInt(request.getParameter("shopId"));
            String page= request.getParameter("page");
            String size=request.getParameter("size");
            PageHelper.startPage(page ==null?1:Integer.parseInt(page),size==null?5:Integer.parseInt(size));

            ProductService productService =new ProductServiceImpl();

            PrintWriter out = response.getWriter();
            try {

                List<ProductDO> list  =productService.findAllByProductName(shopId,productName);

                PageInfo pageInfo=new PageInfo(list);
                out.print(ResultVO.success().add(pageInfo).toJson());
            } catch (OurException e) {
               out.print(ResultVO.fail(e.getCodeEnum()).toJson());
            }catch (Exception e1){
               out.print(ResultVO.fail(ProductEnum.PRODUCT_Find_ENUM).toJson());
            }finally {
               out.close();
            }

    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }
}
